import '../../domain/entities/user_entity.dart';

class LocationModel extends LocationEntity {
  const LocationModel({super.city, super.country, super.coordinates});

  factory LocationModel.fromJson(Map<String, dynamic> json) {
    return LocationModel(
      city: json['city'],
      country: json['country'],
      coordinates:
          json['coordinates'] != null
              ? List<double>.from(json['coordinates'])
              : null,
    );
  }

  @override
  Map<String, dynamic> toJson() {
    return {'city': city, 'country': country, 'coordinates': coordinates};
  }
}

class UserPreferencesModel extends UserPreferencesEntity {
  const UserPreferencesModel({
    super.language,
    super.theme,
    super.notifications,
  });

  factory UserPreferencesModel.fromJson(Map<String, dynamic> json) {
    return UserPreferencesModel(
      language: json['language'] ?? 'zh',
      theme: json['theme'] ?? 'auto',
      notifications:
          json['notifications'] != null
              ? Map<String, bool>.from(json['notifications'])
              : const {
                'email': true,
                'push': true,
                'follows': true,
                'likes': true,
                'comments': true,
              },
    );
  }

  @override
  Map<String, dynamic> toJson() {
    return {
      'language': language,
      'theme': theme,
      'notifications': notifications,
    };
  }
}

class UserModel extends UserEntity {
  const UserModel({
    required super.id,
    required super.username,
    required super.name,
    required super.email,
    super.avatar,
    super.bio,
    super.location,
    super.followers,
    super.following,
    super.followersCount,
    super.followingCount,
    super.isVerified,
    super.isActive,
    super.lastLoginAt,
    super.preferences,
    required super.createdAt,
    super.updatedAt,
  });

  factory UserModel.fromJson(Map<String, dynamic> json) {
    return UserModel(
      id: json['_id'] ?? json['id'] ?? '',
      username: json['username'] ?? '',
      name: json['name'] ?? json['username'] ?? '',
      email: json['email'] ?? '',
      avatar: json['avatar'],
      bio: json['bio'],
      location:
          json['location'] != null
              ? LocationModel.fromJson(json['location'])
              : null,
      followers: List<String>.from(json['followers'] ?? []),
      following: List<String>.from(json['following'] ?? []),
      followersCount: json['followersCount'] ?? 0,
      followingCount: json['followingCount'] ?? 0,
      isVerified: json['isVerified'] ?? false,
      isActive: json['isActive'] ?? true,
      lastLoginAt:
          json['lastLoginAt'] != null
              ? DateTime.parse(json['lastLoginAt'])
              : null,
      preferences:
          json['preferences'] != null
              ? UserPreferencesModel.fromJson(json['preferences'])
              : const UserPreferencesModel(),
      createdAt:
          json['createdAt'] != null
              ? DateTime.parse(json['createdAt'])
              : (json['created_at'] != null
                  ? DateTime.parse(json['created_at'])
                  : DateTime.now()),
      updatedAt:
          json['updatedAt'] != null
              ? DateTime.parse(json['updatedAt'])
              : (json['updated_at'] != null
                  ? DateTime.parse(json['updated_at'])
                  : null),
    );
  }

  @override
  Map<String, dynamic> toJson() {
    return {
      '_id': id,
      'username': username,
      'name': name,
      'email': email,
      'avatar': avatar,
      'bio': bio,
      'location':
          location != null && location is LocationModel
              ? (location as LocationModel).toJson()
              : null,
      'followers': followers,
      'following': following,
      'followersCount': followersCount,
      'followingCount': followingCount,
      'isVerified': isVerified,
      'isActive': isActive,
      'lastLoginAt': lastLoginAt?.toIso8601String(),
      'preferences':
          preferences is UserPreferencesModel
              ? (preferences as UserPreferencesModel).toJson()
              : null,
      'createdAt': createdAt.toIso8601String(),
      'updatedAt': updatedAt?.toIso8601String(),
    };
  }

  UserEntity toEntity() {
    return UserEntity(
      id: id,
      username: username,
      name: name,
      email: email,
      avatar: avatar,
      bio: bio,
      location: location,
      followers: followers,
      following: following,
      followersCount: followersCount,
      followingCount: followingCount,
      isVerified: isVerified,
      isActive: isActive,
      lastLoginAt: lastLoginAt,
      preferences: preferences,
      createdAt: createdAt,
      updatedAt: updatedAt,
    );
  }
}
